package com.smart.tool.extension.impl;

import java.util.List;

import com.smart.tool.extension.IExtensionFinder;
import com.smart.tool.extension.SenceParam;
import com.smart.tool.extension.repository.IExtensionRepository;
public class ExtensionFinderImpl implements IExtensionFinder {



    private IExtensionRepository iExtensionRepository;

    public ExtensionFinderImpl(IExtensionRepository iExtensionRepository) {
        this.iExtensionRepository = iExtensionRepository;
    }

    @Override
    public <T> List<T> getSenceExtensionList(Class<T> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("clazz is null");
        }
        return (List<T>)iExtensionRepository.getExtensionList(clazz);
    }

    @Override
    public Object getExtension(SenceParam senceParam) {
        return iExtensionRepository.getExtension(senceParam,Object.class);
    }

    @Override
    public <T> T getExtension(SenceParam senceParam, Class<T> clazz) {
        checkParams(senceParam, clazz);
        return (T) iExtensionRepository.getExtension(senceParam, clazz);
    }

    @Override
    public <T> List<T> getSenceExtensionList(SenceParam senceParam, Class<T> clazz) {
        checkParams(senceParam, clazz);
        return (List<T>) iExtensionRepository.getExtensionList(senceParam, clazz);
    }

    private static <T> void checkParams(SenceParam senceParam, Class<T> clazz) {
        String sence = senceParam.getSence();
        if (sence == null || "".equals(sence.trim())) {
            throw new IllegalArgumentException("sence is null");
        }
        String useCase = senceParam.getUseCase();
        if (useCase == null || "".equals(useCase.trim())) {
            throw new IllegalArgumentException("useCase is null");
        }
        if (clazz == null) {
            throw new IllegalArgumentException("clazz is null");
        }
    }
}
